package com.mjk.rest.system;

import java.security.acl.Group;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.mjk.common.biz.BaseBiz;
import com.mjk.common.rest.BaseController;
import com.mjk.common.util.NumberUtil;
import com.mjk.common.util.TreeUtil;
import com.mjk.common.vo.ObjectRestResponse;
import com.mjk.common.vo.TableResponse;
import com.mjk.constant.AdminCommonConstant;
import com.mjk.model.system.BaseGroup;
import com.mjk.service.system.BaseGroupService;
import com.mjk.vo.AuthorityMenuTree;
import com.mjk.vo.GroupTree;
import com.mjk.vo.GroupUsers;

import tk.mybatis.mapper.entity.Example;

@RestController
@RequestMapping("/api/group")
public class BaseGroupController extends BaseController<BaseGroupService, BaseGroup>{
	
	@Autowired
	private BaseGroupService baseGroupService;
	
	@RequestMapping(value = "/getAll", method = RequestMethod.GET)
	public List<BaseGroup> getAll() {
		// TODO Auto-generated method stub
		return baseGroupService.getAll(null);
	}
	
	@RequestMapping(value = "/{id}/authority/menu", method = RequestMethod.GET)
	public ObjectRestResponse<List<AuthorityMenuTree>> getMenuAuthority(@PathVariable int id){
		return new ObjectRestResponse<>().data(baseGroupService.getAuthorityMenu(id)).flag(true);
	}
	
	@RequestMapping(value = "/{id}/authority/menu", method = RequestMethod.POST)
	public ObjectRestResponse modifyMenuAuthority(@PathVariable int id, @RequestBody Map<String, String> menuTrees){
		String[] menus = menuTrees.get("menuTrees").split(",");
		baseGroupService.editAuthorityMenu(id, menus);
		return new ObjectRestResponse<>().flag(true);
	}
	
	@RequestMapping(value = "/{id}/authority/element", method = RequestMethod.GET)
	public ObjectRestResponse<List<Integer>> getElementAuthority(@PathVariable int id){
		return new ObjectRestResponse<>().data(baseGroupService.getAuthorityElement(id)).flag(true);
	}
	
	@RequestMapping(value = "/{id}/authority/element/add", method = RequestMethod.POST)
    public ObjectRestResponse addElementAuthority(@PathVariable  int id, @RequestBody Map<String, Integer> params){
		baseGroupService.editAuthorityElement(id, params.get("menuId"), params.get("elementId"));
        return new ObjectRestResponse().flag(true);
    }
	
	@RequestMapping(value = "/{id}/authority/element/remove", method = RequestMethod.POST)
    public ObjectRestResponse removeElementAuthority(@PathVariable int id, @RequestBody Map<String, Integer> params){
		baseGroupService.removeAuthorityElement(id, params.get("menuId"), params.get("elementId"));
        return new ObjectRestResponse().flag(true);
    }
	
	@RequestMapping(value = "/{id}/user", method = RequestMethod.GET)
    public ObjectRestResponse<GroupUsers> getUsers(@PathVariable int id){
		 return new ObjectRestResponse<GroupUsers>().flag(true).data(baseBiz.getGroupUsers(id));
    }
	
	@RequestMapping(value = "/{id}/user", method = RequestMethod.PUT)
    public ObjectRestResponse<GroupUsers> modifyUsers(@PathVariable int id,@RequestBody Map<String, String> params){
		baseGroupService.editGroupUsers(id, params.get("members"), params.get("leaders"));
        return new ObjectRestResponse().flag(true);
    }

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public TableResponse<BaseGroup> list(Map<String, Object> params) {
		// TODO Auto-generated method stub
		return super.list(params);
	}
	
	@RequestMapping(value = "/tree", method = RequestMethod.GET)
	public List<GroupTree> tree(String name, String groupType){
		if(StringUtils.isBlank(name) && StringUtils.isBlank(groupType)){
			return new ArrayList<GroupTree>();
		}
		Example example = new Example(BaseGroup.class);
		if(StringUtils.isNotBlank(name)){
			example.createCriteria().andLike("name", "%" + name + "%");
		}
		if(StringUtils.isNotBlank(groupType)){
			example.createCriteria().andEqualTo("groupType", groupType);
		}
		return getTree(baseBiz.selectByExample(example), AdminCommonConstant.ROOT);
	}
	
	private List<GroupTree> getTree(List<BaseGroup> list, int root){
		List<GroupTree> groupTree = new ArrayList<GroupTree>();
		GroupTree temp = null;
		for (BaseGroup group : list) {
			temp = new GroupTree();
			temp.setLabel(group.getName());
			BeanUtils.copyProperties(group, temp);
			groupTree.add(temp);
		}
		return TreeUtil.bulid(groupTree, root);
	}
}
